<template>
  <div v-loading="tableLoading" class="app-container">
    <div class="search-box">
      <el-form ref="searchForm" :inline="true" :model="formFilter">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formFilter.name" />
        </el-form-item>
        <el-form-item label="账号" prop="loginName">
          <el-input v-model="formFilter.loginName" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
          <el-button icon="el-icon-remove-outline" title="重置" @click="resetSearchForm" />
        </el-form-item>
      </el-form>
    </div>

    <el-table
      ref="multipleTable"
      stripe
      :data="tableData"
      border
      class="table"
    >
      <el-table-column prop="loginName" label="账号" width="120" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="departmentName" label="所属部门" />
      <el-table-column prop="duty" label="职务" />
      <el-table-column prop="roles" label="拥有角色">
        <template slot-scope="scope">
          <span>{{ scope.row.roles |  }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ip" label="ip地址" />
      <el-table-column prop="loginTime" label="登录时间">
        <template slot-scope="scope">
          <span>{{ scope.row.loginTime | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160" align="left">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-delete"
            class="red inline-btn"
            size="mini"
            @click="handleDelete(scope.row)"
          >踢出</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="curPage" :limit.sync="limit" @pagination="getList" />
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'
export default {
  name: 'OnlineUser',
  components: { Pagination },
  data() {
    return {
      tableLoading: true,
      tableData: [],
      curPage: 1,
      limit: 5,
      total: 0,
      formFilter: {
        name: null,
        loginName: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取数据
    getList() {
      this.tableLoading = true
      const param = {}
      this.copyNotBlankField(this.formFilter, param)

      const queryPage = {}
      queryPage.limit = this.limit
      queryPage.offset = this.limit * (this.curPage - 1)
      param.page = queryPage
      request({ url: '/sys/onlineuser/findPage', method: 'post', data: param })
        .then(res => {
          this.tableLoading = false
          this.tableData = res.rows
          this.total = res.total
          this.curPage = res.pageNumber
        })
        .catch(e => {
          this.tableLoading = false
          this.$message.error('查询出错')
        })
    },
    onSearch() {
      this.getList()
    },
    resetSearchForm() {
      this.$refs['searchForm'].resetFields()
    },
    handleDelete(row) {
      this.$confirm('此操作将踢出用户[' + row.name + '], 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDelete(row.enToken)
        })
    },
    doDelete(enToken) {
      this.tableLoading = true
      const param = {}
      param.enToken = enToken
      request({ url: '/sys/onlineuser/kickOut', method: 'post', params: param })
        .then(res => {
          this.tableLoading = false
          this.getList()
        })
        .catch(e => {
          this.tableLoading = false
          this.$message.error('踢出用户出错！' + e.message ? e.message : '')
        })
    }

  }
}
</script>
